<template>
  <div id="address">
    <!-- 修改用户的对话框 -->
    <el-dialog title="修改地址" :visible.sync="dialog" :before-close="closeDialog" width="35%">
      <el-form ref="addressFormRef" :rules="addressFormRules" :model="editForm" label-position="right" label-width="auto" style="padding: 0 60px;">
        <el-form-item label="省市区/县" prop="address1">
          <el-cascader v-model="addressForm.address1" :options="cityData" />
        </el-form-item>
        <el-form-item label="详细地址" prop="address2">
          <el-input v-model="addressForm.address2" />
        </el-form-item>
      </el-form>
      <!-- 底部区域 -->
      <div slot="footer" class="dialog-footer">
        <el-button @click="closeDialog">
          取消
        </el-button>
        <el-button type="primary" @click="confirmDialog">
          确定
        </el-button>
      </div>
    </el-dialog>

  </div>

</template>

<script>
import cityData from '@/utils/citydata'
export default {
  name: 'AddressItem',
  props: {
    // 组件接收父级传递的控制弹框 打开/关闭的状态值
    dialog: {
      type: Boolean,
      default: null
    },
    value: {
      type: Object,
      default: null
    }
  },
  data() {
    return {
      addressForm: {
        address1: [],
        address2: ''
      },
      addressFormRules: {
        address1: [
          { required: true, message: '请选择省市区/县', trigger: 'blur' }
        ],
        address2: [
          { required: true, message: '请填写详细地址', trigger: 'blur' }
        ]
      },
      cityData: cityData
    }
  },

  methods: {
    // 点击取消
    closeDialog() {
      this.$emit('close')
    },
    // 点击确认，处理提交表单，向父级传递
    confirmDialog() {
      this.$refs['editFormRef'].validate((valid) => {
        if (valid) {
          // 定义一个变量为 editData 用来接收拷贝的 this.temp
          const editData = Object.assign({}, this.editForm)
          // console.log(editData)，打印看里面的字段
          this.$emit('edit', editData)
        }
      })
    }
  }
}
</script>
